package com.ruoyi.ebike.service.impl;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ebike.mapper.OrdersMapper;
import com.ruoyi.ebike.domain.Orders;
import com.ruoyi.ebike.service.IOrdersService;

/**
 * 骑行记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 查询骑行记录
     * 
     * @param id 骑行记录主键
     * @return 骑行记录
     */
    @Override
    public Orders selectOrdersById(Long id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询骑行记录列表
     * 
     * @param orders 骑行记录
     * @return 骑行记录
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增骑行记录
     * 
     * @param orders 骑行记录
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改骑行记录
     * 
     * @param orders 骑行记录
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除骑行记录
     * 
     * @param ids 需要删除的骑行记录主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除骑行记录信息
     * 
     * @param id 骑行记录主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id)
    {
        return ordersMapper.deleteOrdersById(id);
    }

    @Override
    public Orders selectOrdersByVehicleId(Long vehicleId) {

        return ordersMapper.selectOrdersByVehicleId(vehicleId);
    }


/*    public Map<String, BigDecimal> getOrderStats(String timeRange) {
        LocalDateTime startTime, endTime;
        switch (timeRange) {
            case "today":
                startTime = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
                endTime = startTime.plusDays(1);
                break;
            case "thisWeek":
                startTime = LocalDateTime.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                endTime = LocalDateTime.now();
                break;
            case "last7Days":
                startTime = LocalDateTime.now().minusDays(7);
                endTime = LocalDateTime.now();
                break;
            case "thisMonth":
                startTime = LocalDateTime.now().withDayOfMonth(1);
                endTime = startTime.plusMonths(1);
                break;
            case "allTime":
                startTime = LocalDateTime.of(2024, Month.JANUARY, 1, 0, 0);
                endTime = LocalDateTime.now();
                break;
            default:
                throw new IllegalArgumentException("Invalid time range: " + timeRange);
        }

        Map<String, BigDecimal> stats = new HashMap<>();
        stats.put("cycle", ordersMapper.calculateCycleIncome(startTime, endTime));
        stats.put("coupon", ordersMapper.calculateCouponIncome(startTime, endTime));
        stats.put("total", stats.get("cycle").add(stats.get("coupon")));
        return stats;
    }*/


    public Map<String, BigDecimal> getOrderStats(String timeRange) {
        LocalDateTime startTime, endTime;
        switch (timeRange) {
            case "today":
                startTime = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
                endTime = startTime.plusDays(1);
                break;
            case "thisWeek":
                startTime = LocalDateTime.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).truncatedTo(ChronoUnit.DAYS);
                endTime = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS).plusDays(1);
                break; // 注意：这里假设一周的结束时间是下一天的开始，可能需要根据实际需求调整
            case "last7Days":
                startTime = LocalDateTime.now().minusDays(7);
                endTime = LocalDateTime.now();
                break;
            case "thisMonth":
                startTime = LocalDateTime.now().withDayOfMonth(1);
                endTime = startTime.plusMonths(1);
                break;
            case "allTime":
                startTime = LocalDateTime.of(2024, Month.JANUARY, 1, 0, 0);
                endTime = LocalDateTime.now();
                break;
            default:
                throw new IllegalArgumentException("Invalid time range: " + timeRange);
        }

        BigDecimal cycleIncome = Optional.ofNullable(ordersMapper.calculateCycleIncome(startTime, endTime)).orElse(BigDecimal.ZERO);
        BigDecimal couponIncome = Optional.ofNullable(ordersMapper.calculateCouponIncome(startTime, endTime)).orElse(BigDecimal.ZERO);

        Map<String, BigDecimal> stats = new HashMap<>();
        stats.put("cycle", cycleIncome);
        stats.put("coupon", couponIncome);
        stats.put("total", cycleIncome.add(couponIncome));

        return stats;
    }

    public Map<String, BigDecimal> getOrderCount(String timeRange) {
        LocalDateTime startTime, endTime;
        switch (timeRange) {
            case "today":
                startTime = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
                endTime = startTime.plusDays(1);
                break;
            case "allTime":
                startTime = LocalDateTime.of(2024, Month.JANUARY, 1, 0, 0);
                endTime = LocalDateTime.now();
                break;
            default:
                throw new IllegalArgumentException("Invalid time range: " + timeRange);
        }

        BigDecimal orderCount = Optional.ofNullable(ordersMapper.calculateOrderCount(startTime, endTime)).orElse(BigDecimal.ZERO);

        Map<String, BigDecimal> stats = new HashMap<>();
        stats.put("count", orderCount);
        return stats;
    }

    @Override
    public BigDecimal getTotalRidingDistance() {
        return ordersMapper.getTotalRidingDistance();
    }
}
